package com.android.webrtcsdkdev.settingDialog;

import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.webrtcsdkdev.R;

public class HideAppDialog implements View.OnClickListener {


    private Dialog dialog;
    private Context mContext;
    private EditText edt_instance_id, edt_ip, edt_agent_token;
    private Button bt_send, bt_cancel;
    private IRequesListener2 mListener1;


    public void show(final Context context, IRequesListener2 listener) {
        try {
            mContext = context;
            mListener1 = listener;
            dialog = new Dialog(mContext, R.style.Dialog_Ctrl);
            dialog.setContentView(R.layout.hide_app_dialog);
            edt_instance_id = dialog.findViewById(R.id.edt_instance_id);
            edt_ip = dialog.findViewById(R.id.edt_ip);
            edt_agent_token = dialog.findViewById(R.id.edt_agent_token);
            bt_send = dialog.findViewById(R.id.bt_send);
            bt_cancel = dialog.findViewById(R.id.bt_cancel);
            bt_send.setOnClickListener(this);
            bt_cancel.setOnClickListener(this);

            Window window = dialog.getWindow();
            window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            dialog.show();
        } catch (Exception e) {
            Log.d("HideAppDialog", "HideAppDialog Exception:" + e.getMessage());
        }
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.bt_send) {
            String instanceId = edt_instance_id.getText().toString().trim();
            String ip = edt_ip.getText().toString().trim();
            String agentToken = edt_agent_token.getText().toString().trim();

            if (instanceId.isEmpty() || ip.isEmpty() || agentToken.isEmpty()) {
                Toast.makeText(mContext, "请求参数不能为空", Toast.LENGTH_SHORT).show();
                return;
            }
            mListener1.onRequestParam(instanceId, ip, agentToken);
            dismissDialog();
        } else if (id == R.id.bt_cancel) {
            dismissDialog();
        }
    }


    public void dismissDialog() {
        try {
            if (dialog != null && dialog.isShowing()) {
                dialog.dismiss();
            }
        } catch (Exception e) {

        }
    }
}
